package com.febers.ihealth

import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.viewpager.widget.ViewPager
import com.febers.ihealth.base.BaseActivity
import com.febers.ihealth.entity.ReleaseNextSurveyEvent
import com.febers.ihealth.entity.SurveyFinishEvent
import com.febers.ihealth.utils.*
import com.febers.ihealth.view.LimitSlideDirectionViewPager
import com.febers.ihealth.view.SurveyFragmentAdapter
import kotlinx.android.synthetic.main.activity_survey_container.*
import kotlinx.android.synthetic.main.toolbar_common.*

class SurveyContainer : BaseActivity(), EventListener {

    private var order: Int = 0
        set(value) {
            field = value
            if (field > 0) {
                btn_pre_survey.visibility = View.VISIBLE
            } else {
                btn_pre_survey.visibility = View.GONE
            }
        }

    private var canNext = false

    override fun initView() {
        view_pager_survey.adapter = SurveyFragmentAdapter(supportFragmentManager)
        view_pager_survey.currentItem = 0
        order = 0
        view_pager_survey.setAllowedSwipeDirection(LimitSlideDirectionViewPager.SwipeDirection.LEFT)
        view_pager_survey.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) { }
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { }
            override fun onPageSelected(position: Int) {
                //如果下一个问卷，下一个的按钮将被重置
                log("order: $order p: $position")
                if (position > order) {
                    order++
                } else {
                    order--
                }
                if (position == (view_pager_survey.adapter?.count ?: 0) - 1) {
                    btn_next_survey.visibility = View.GONE
                } else {
                    btn_next_survey.visibility = View.VISIBLE
                }
            }
        })
        btn_pre_survey.setOnClickListener {
            if (order == 0) {
                finish()
            } else {
                val pre = order-1
                view_pager_survey.setCurrentItem(pre, true)
            }
        }
        btn_next_survey.setOnClickListener {
            if (canNext) {
                val next = order+1
                view_pager_survey.setCurrentItem(next, true)
            } else {
                toast("请您完成必要信息的选择")
            }
        }
    }

    override fun afterCreate() {
    }

    override fun onEvent(event: Any, fromParents: Boolean) {
        if (event is ReleaseNextSurveyEvent) {
            if (event.release) {
                canNext = true
                view_pager_survey.setAllowedSwipeDirection(LimitSlideDirectionViewPager.SwipeDirection.ALL)
            } else {
                canNext = false
                view_pager_survey.setAllowedSwipeDirection(LimitSlideDirectionViewPager.SwipeDirection.LEFT)
            }
        }
        if (event is SurveyFinishEvent) {
            if (event.finish) {
                finish()
            }
        }
    }

    override fun setView(): Int = R.layout.activity_survey_container

    override fun setToolbar(): Toolbar? = toolbar_common

    override fun setToolbarTitle(): String? = "问卷调查"

    override fun onStart() {
        super.onStart()
        Event.register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        Event.unregister(this)
    }
}
